package com.eqdd.yiqidian.ui.tongxunlu;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.TeamCheatListActivityCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.GroupListResultBean;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonCallBack;
import com.eqdd.yiqidian.utils.ImageUtil;
import com.eqdd.yiqidian.utils.RefreshUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;
import io.rong.imkit.RongIM;

/**
 * Created by lvzhihao on 17-4-10.
 */

public class TeamCheatListActivity extends BaseActivity {

    private TeamCheatListActivityCustom dataBinding;

    private ArrayList<Object> datas;
    private SlimAdapterEx slimAdapterEx;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_team_cheat_list);
        initTopTitleBar(View.VISIBLE, "群管理");
    }

    @Override
    public void initData() {
        datas = new ArrayList<>();
        datas.add("创建群");
    }

    @Override
    public void setView() {

        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        dataBinding.recyclerView.setHasFixedSize(true);
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.item_chatroom_header, new SlimInjector<String>() {
            @Override
            public void onInject(String data, IViewInjector injector) {
                injector.text(R.id.tv_header, data);
            }
        })
                .register(R.layout.list_item_tongxunlu, new SlimInjector<GroupListResultBean>() {
                    @Override
                    public void onInject(GroupListResultBean data, IViewInjector injector) {
                        injector.text(R.id.tv_content, data.getGroupname())
                                .with(R.id.iv_head, v ->
                                        ImageUtil.setCircleImage(data.getGroupphoto(), (ImageView) v)
                                );
                    }
                })
                .attachTo(dataBinding.recyclerView).updateData(datas);

        RefreshUtil.openRefresh(dataBinding.coolRefreshView, coolRefreshView -> {
            OkGo.<HttpResult<List<GroupListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.GROUP_LIST_INFO)
                    .params("userGuid", user.getGuid())
                    .execute(new JsonCallBack<HttpResult<List<GroupListResultBean>>>() {
                        @Override
                        public void onSuccess(Response<HttpResult<List<GroupListResultBean>>> response) {
                            HttpResult<List<GroupListResultBean>> httpResult = response.body();
                           // ToastUtil.showShort(httpResult.getMsg());
                            dataBinding.coolRefreshView.setRefreshing(false);
                            if (httpResult.getStatus() == 200) {
                                datas.clear();
                                datas.add("创建群");
                                datas.addAll(httpResult.getItems());
                                slimAdapterEx.updateData(datas);
                            }
                        }

                        @Override
                        public void onError(Response<HttpResult<List<GroupListResultBean>>> response) {
                            super.onError(response);
                            dataBinding.coolRefreshView.setRefreshing(false);
                        }
                    });
        });
        dataBinding.coolRefreshView.setRefreshing(true);
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((rv, p, v) -> {
                    switch (p) {
                        case 0:
                            startActivity(AddGroupChatActivity.class);
                            break;
//                        case 1:
//                            startActivityForResult(SearchQunActivity.class, null, Config.QUN_GUANLI);
//                            break;
                        default:
                            GroupListResultBean bean = (GroupListResultBean) slimAdapterEx.getData().get(p);
                            RongIM.getInstance().startGroupChat(this, bean.getGroupid(),
                                    bean.getGroupname());
                            break;
                    }
                });
        dataBinding.etKey.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                ArrayList<Object> temp = new ArrayList<>();
                Flowable.fromIterable(datas)
                        .filter(o -> {
                            if (o instanceof String) {
                                return true;
                            } else {
                                GroupListResultBean item = (GroupListResultBean) o;
                                return item.getGroupname().contains(s);
                            }
                        })
                        .subscribe(temp::add, System.out::println, () -> slimAdapterEx.updateData(temp));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.CHANGE) {
            dataBinding.coolRefreshView.setRefreshing(true);
        }
    }
}
